Ccay.Module.define("Ccay.example.ui.igird.customEditor", function (page, $S) {
	
	var arrayAttr3 = ["降级", "降薪", "饱和配股数", "饱和配股资格第1次", "饱和配股资格第2次", "罚款", "其他"];
	
	page.ready = function(){
		var datas = [
			{"attr1": "ww", "attr2": "wwwwww", "attr3": null, "attr4": null},
			{"attr1": "ww", "attr2": "wwwwww", "attr3": "1", "attr4": "1:ww"},
			{"attr1": "ww", "attr2": "wwwwww", "attr3": "2,4", "attr4": "2:ww;4:ee"},
			{"attr1": "ww", "attr2": "wwwwww", "attr3": "2,5,6", "attr4": "2:ww;5:ee;6:ee"},
			{"attr1": "ww", "attr2": "wwwwww", "attr3": "1,2,3,4", "attr4": "1:ww;2:ee;3:ww;4:ee"}
		],
		renderAttr3 = {
			getFormat : function(field, format) {
				return "${#Ccay.example.ui.igird.customEditor.renderAttr3({0})}".formatText(field);
			},
			getValue : function(target, format) {
				var inputText = [];
				target.children("input").each(function (i, checkbox) {
					if (checkbox.checked) {
						inputText.push(checkbox.value);
					}
				});
				return inputText.join(",");
			},
			format : function(value, format) {
				return page.renderAttr3(value);
			},
			render : function(target, value, format) {
				target.html(this.format(value, format));
			}
		},
		editorAttr3 = {
			build : function(container, op) {
				var ctr = $.create("div").width(200).appendTo(container);
				ctr.html(page.renderAttr3(null, true));
				ctr.children("input").change(function (e) {
					var curTarget = e.currentTarget,
						txtTarget = $("#txtEditor" + curTarget.value);
					if (curTarget.checked) {
						txtTarget.removeAttr("disabled");
					} else {
						txtTarget.val("");
						txtTarget.attr("disabled", true);
					}
				});
				return ctr;	
			},
			setValue : function(ctr, value) {
				var valueArray = value && value.split(",");
				if (valueArray) {
					ctr.children("input").each(function (i, checkbox) {
						$.each(valueArray, function (v, val) {
							if (val == checkbox.value) {
								checkbox.checked = true;
								valueArray.shift();
								return false;
							}
						});
					});
				}
			},
			getValue : function(ctr) {
				var ctrValue = [];
				ctr.children("input").each(function (i, checkbox) {
					if (checkbox.checked) {
						ctrValue.push(checkbox.value);
						checkbox.checked = false;
					}
				});
				return ctrValue.join(",");
			}
		},
		renderAttr4 = {
			getFormat : function(field, format) {
				return "${#Ccay.example.ui.igird.customEditor.renderAttr4({0})}".formatText(field);
			},
			getValue : function(target, format) {
				var inputText = [], spanDom;
				target.children("span").each(function (i, spantext) {
					spanDom = $(spantext);
					if (spanDom.text()) {
						inputText.push(spanDom.attr("val") + ":" + spanDom.text());
					}
				});
				return inputText.join(";");
			},
			format : function(value, format) {
				return page.renderAttr4(value);
			},
			render : function(target, value, format) {
				target.html(this.format(value, format));
			}
		},
		editorAttr4 = {
			build : function(container, op) {
				var ctr = $.create("div").width(200).appendTo(container);
				ctr.html(page.renderAttr4(null, true));
				return ctr;	
			},
			setValue : function(ctr, value) {
				var valueArray = value && value.split(";"), dataArray, isHasValue, inputDom;
				ctr.children("input").each(function (i, inputtext) {
					inputDom = $(inputtext);
					isHasValue = false;
					if (valueArray) {
						$.each(valueArray, function (d, data) {
							dataArray = data.split(":");
							if (inputDom.attr("val") == dataArray[0]) {
								inputDom.val(dataArray[1]);
								valueArray.shift();
								isHasValue = true;
								return false;
							}
						});
					}
					!isHasValue && inputDom.attr("disabled", true);
				});
			},
			getValue : function(ctr) {
				var ctrValue = [], inputDom;
				ctr.children("input").each(function (i, inputtext) {
					inputDom = $(inputtext);
					if (inputDom.val()) {
						ctrValue.push(inputDom.attr("val") + ":" + inputDom.val());
						inputDom.val("");
					} else {
						inputDom.removeAttr("disabled");
					}
				});
				return ctrValue.join(";");
			}
		},
		gridOption = {
			height: 500,
			editMode: true,
			rowEdit: true,
			wrap: true,
			columns: [
				{columnType: "select", multi: true, freezeable: true},
		 		{field: "attr1", width:100, header: "Attr1"},
		 		{field: "attr2", width:150, header: "Attr2", type: "longtext"},
		 		{field: "attr3", width:200, header: "Attr3", renderer: Ccay.UI.Renderer.create(renderAttr3), editor: Ccay.Editor.create(editorAttr3)},
		 		{field: "attr4", width:200, header: "Attr4", renderer: Ccay.UI.Renderer.create(renderAttr4), editor: Ccay.Editor.create(editorAttr4)}
		    ]
		};
		
		$S("#grdEditor").igrid(gridOption, datas);
		
		$S("#customEditorDemo").ccayTab();
	};	
	
	page.renderAttr3 = function (field, noDisabled) {
		var html = [], fieldArray = field && field.split(",");
		$.each(arrayAttr3, function (d, data) {
			var isChecked;
			if (fieldArray) {
				$.each(fieldArray, function (r, record) {
					if (d + 1 == record) {
						isChecked = "checked='true'";
						return false;
					}
				});
			}
			html.push("<input type='checkbox' value='{3}' {2} {1} /><span>{0}</span><br>".formatText(data, !noDisabled ? "disabled='true'" : "", isChecked || "", (d + 1)));
		});
		return html.join("");
	};
	
	page.renderAttr4 = function (field, isInput) {
		var html = [], fieldArray = field && field.split(";");
		for (var i = 1, len = arrayAttr3.length; i <= len; i++) {
			if (fieldArray) {
				var inputText;
				$.each(fieldArray, function (r, record) {
					recordArray = record.split(":");
					if (i == recordArray[0]) {
						inputText = recordArray[1];
						return false;
					} else {
						inputText = "";
					}
				});
				html.push("<span val='{1}'>{0}</span><br>".formatText(inputText, i));
			}
			if (isInput) {
				html.push("<input id='txtEditor{0}' type='text' val='{0}'/><br>".formatText(i));
			}
		}
		return html.join("");
	};
	
	page.operateTest = function (value) {
		alert(Ccay.DataHelper.toJsonStr(value));
	};
});